﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace winformLesson1
{
    public partial class UpdatePwd : Form
    {
        private User user;
        public UpdatePwd()
        {
            InitializeComponent();
        }
        public UpdatePwd(User user)
        {
            InitializeComponent();
            this.user = user;
        }

        private void btn_Update_Click(object sender, EventArgs e)
        {
            // 1、采集数据
            string userpwd = tbx_UserPwd.Text;
            string newuserpwd = tbx_UserNewPwd.Text;
            string renewuserpwd = tbx_UserReNewPwd.Text;

            // 2、判断账号密码是否为空
            if (string.IsNullOrEmpty(userpwd) || string.IsNullOrEmpty(userpwd) || string.IsNullOrEmpty(userpwd))
            {
                MessageBox.Show("密码不能为空！");
                return;
            }
            if (newuserpwd != renewuserpwd)
            {
                MessageBox.Show("两次密码不一致！");
                return;
            }
            if (UpdateUserPwd(userpwd, newuserpwd))
            {
                MessageBox.Show("修改成功");
            }
            else
            {
                MessageBox.Show("修改失败");
            }
        }

        private bool UpdateUserPwd(string userpwd, string newuserpwd)
        {
            string sql = string.Format("update [User] set UserPwd=@NewUserPwd where UserId=@UserId and UserPwd=@OldUserPwd");
            SqlParameter[] paras = new SqlParameter[]
            {
                new SqlParameter("@UserId",user.UserId),
                new SqlParameter("@OldUserPwd",userpwd),
                new SqlParameter("@NewUserPwd",newuserpwd)
            };
            return SqlHelper.ExecuteNonQuery(sql, paras) > 0 ? true : false;
        }

        private void UpdatePwd_Load(object sender, EventArgs e)
        {

        }
    }
}
